<template>
  <div @keyup.enter="getData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="项目名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          :auto-top="true"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目立项')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.city_res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.city_res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.city_res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.city_res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.city_res_a_name }}</div>
              <div>B角:{{ scope.row.city_res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
      <template v-slot:letter="scope">
        <div
          class="badge cursor-pointer"
          :class="`badge-${scope.row.letterStatusStyle}`"
          @click="showLetter(scope.row)"
        >
          <div v-if="+scope.row.confuse_status === 3" :title="scope.row.confuse_des">
            {{ scope.row.letterStatusLabel }}
          </div>
          <span v-else>{{ scope.row.letterStatusLabel }}</span>
        </div>
      </template>
      <template v-slot:com="scope">
        <div>
          <small class="text-muted">{{ scope.row.service_name || '-' }}</small>
        </div>
        <div>
          <small class="text-muted">{{ scope.row.bank_name || '-' }}</small>
        </div>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <view-dialog :visible.sync="viewVisible" :related-data="viewRow"></view-dialog>

    <el-dialog :visible.sync="letterVisible" width="840px">
      <letter-view :query-id="letterQueryId"></letter-view>
    </el-dialog>

    <approval-edit
      :visible.sync="approveVisible"
      :table-id="relatedId"
      :form-data="editData"
      @success="reGetData"
    ></approval-edit>
  </div>
</template>

<script>
import { enumGuatype, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/bigcredit/views/accept/mixins'

import SelectAddress from '@/components/select/SelectAddress'
import ViewDialog from '@/bigcredit/views/accept/approval/ViewDialog'
import { enumLetterStatus } from '@/credit/views/city/bus/normal/item/List'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import ApprovalEdit from '@/bigcredit/views/accept/approval/ApprovalEdit'
export default {
  components: {
    SelectAddress,
    ViewDialog,
    LetterView,
    ApprovalEdit,
  },
  mixins: [baseMixins],
  data() {
    return {
      order: {
        N1is_history: 'asc',
        N2item_status: 'asc',
        N3item_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'is_approval_gua.eq': '0',
        subKey: 'ele_letter_info.customer_related_id',
      },

      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '建议金额(万元)', prop: 'approved_funds', align: 'center' } },
        { attrs: { label: '立项时间', prop: 'prelimTime', width: 100 } },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },

        // { attrs: { label: '担保机构', prop: 'service_name', align: 'center', minWidth: '110px' } },
        {
          attrs: { label: '合作机构', prop: 'comOrg', align: 'center', width: 140 },
          slot: 'com',
        },
        {
          attrs: { label: '担保意向函', prop: 'letterStatusLabel', align: 'center' },
          slot: 'letter',
        },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', width: 100 },
          operations: [
            {
              content: '受理',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      viewVisible: false,
      viewRow: {},

      enumStatus: [
        { label: '未立项', value: 0 },
        { label: '已立项', value: 1 },
        { label: '已拒绝', value: 2 },
      ],
      enumParams: {
        0: {
          'item_status.eq': 0,
        },
        1: {
          'item_status.eq': 1,
        },
        2: {
          'item_status.eq': 2,
        },
      },
      selectStatus: '',
      approveVisible: false,
      relatedId: '',
      editData: {},
      letterVisible: false,
      letterQueryId: '',
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getData()
  },
  methods: {
    showLetter(row) {
      if (row.docId) {
        this.letterVisible = true
        this.letterQueryId = row.customer_related_id
      }
    },
    async reGetData() {
      this.getData()
      this.$store.dispatch('getTaskCountBusSelfFast')
    },
    handleEdit(row) {
      this.approveVisible = true
      this.relatedId = row.customer_related_id || ''
      this.editData = row || {}
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type]

      item.prelimTime = this.$utils.dateFormat(item.item_date, this.dateFmt)
      item.resName = `A角: ${item.city_res_a_name} B角: ${item.city_res_b_name}`

      item.processLabel = enumProcessStatus[item.process_status] || ''

      item.item_status = +item.item_status
      item.hasItem = item.item_status === 1

      const letters = (item?.ele_letter_info ?? []).filter(item => +item.ele_letter_type === 1)
      const normalLetter = letters.sort((a, b) => a.sign_status - b.sign_status)
      item.letterStatus = normalLetter?.[0]?.sign_status ?? -1
      const letterStatusObj = enumLetterStatus[item.letterStatus] || {}
      item.docId = normalLetter?.[0]?.doc_id || ''
      item.letterStatusLabel = letterStatusObj.label
      item.letterStatusStyle = letterStatusObj.style
      const isNoSignLetter = letters.filter(item => +item.sign_status < 1)

      const letterNo = isNoSignLetter.length > 0 || +item.item_status === 0 || letters.length === 0
      item.editable =
        [item.res_a, item.res_b].includes(this.userId) && letterNo && +item.confuse_status !== 3

      if (+item.confuse_status === 3) {
        item.letterStatusLabel = '已拒绝'
        item.letterStatusStyle = 'danger'
      }

      item.comOrg = `${item.service_name}-${item.bank_name}`
      return item
    },
    handleView(row) {
      this.viewRow = row
      this.viewVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
